using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using Utilities;

namespace sow
{
	/// <summary>
	/// Summary description for EditPartner.
	/// </summary>
	public partial class EditUserRoles : System.Web.UI.Page
	{
		protected SQLAccess sql; 
		protected DataSet dsRole;
		protected string sSortExpression;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			
			GetSource();		
			if (!IsPostBack)
			{
				
				SQLAccess sql = new SQLAccess();

				DataSet dsUser = new DataSet();
							
				dsUser = sql.GetUsers();
		
				lstUser.DataSource = dsUser;
				lstUser.DataTextField = "NickName";
				lstUser.DataValueField = "UserID";
				lstUser.DataBind();
				lstUser.SelectedIndex = 0;

				DataSet dsRole = new DataSet();
				dsRole = sql.GetRoles();
		
				lstRoleID.DataSource = dsRole;
				lstRoleID.DataTextField = "RoleName";
				lstRoleID.DataValueField = "RoleID";
				lstRoleID.DataBind();
				
				lstRoleID.SelectedIndex = 0;

				lstStatus.Items.Insert(0, "Active");
				lstStatus.Items.Insert(1, "Approved");

						
				BindData();
				DataGrid1.EditItemIndex = -1;
			}
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);

		}
		#endregion

		protected int GetRoleIDIndex(object RoleID)
		{
			if(dsRole == null)
				return -1;
			
			int index = 0;
			foreach( DataRow dr in dsRole.Tables[0].Rows)
			{
				if (dr["RoleID"].ToString() == RoleID.ToString())
					break;
				index++;
			}

			if (index == dsRole.Tables[0].Rows.Count)
				index = -1;
			return index;
		}


		protected void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			//int UserID = (int) DataGrid1.DataKeys[e.Item.ItemIndex];

			if (sql == null)
				sql = new SQLAccess();

			dsRole = sql.GetRoles();
			//DataSet dsUser = new DataSet();
			//dsUser = sql.GetUserPassword(UserID);
			//lblHidden.Text = dsUser.Tables[0].Rows[0]["password"].ToString();
			BindData();			
			lblMessage.Text = "Editing!";
		}

		protected void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			
			int UserID = int.Parse(((TextBox)e.Item.FindControl("txtUserID")).Text);
					
			int RoleID =int.Parse(((DropDownList)e.Item.FindControl("drpRoleID")).SelectedValue); 
			string Email = ((TextBox)e.Item.FindControl("txtEmail")).Text; 
			string Password = ((TextBox)e.Item.FindControl("txtPassword")).Text; 
			string NickName = ((TextBox)e.Item.FindControl("txtNickName")).Text; 
			string Type = ((TextBox)e.Item.FindControl("txtType")).Text; 
			string Status = ((TextBox)e.Item.FindControl("txtStatus")).Text; 
			string Description = ((TextBox)e.Item.FindControl("txtDescription")).Text; 
													
			if (sql == null)
				sql = new SQLAccess();

			//DataSet dsUser = sql.GetUserPasswordByID(UserID);
			//string OldPassword = dsUser.Tables[0].Rows[0]["password"].ToString();

			sql.UpdateUserInfo(UserID, RoleID, Email, Password, NickName, Status, Type,  Description);

			lblMessage.Text = "The record: --" + UserID.ToString() + RoleID.ToString() + 
				Email + Password + NickName + Type + Status + 
				Description + "-- has been updated!!!<br>";
			
			DataGrid1.EditItemIndex = -1;
			
			GetSource();
			BindData();
			
			
		}

		protected void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			BindData();
			//DataGrid1.EditItemIndex = -1;
		}

		
		protected void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int UserID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex]; 
			
			if (sql == null)
				sql = new SQLAccess();

			DataSet dsUser = sql.GetUserByID(UserID);
			string email = dsUser.Tables[0].Rows[0]["Email"].ToString();

			sql.DeleteUser(email);
			
			//lblMessage.Text = "The record of PartnerID = " + UserID.ToString() + " is deleted! <br>";
			
			DataGrid1.EditItemIndex = -1;
			GetSource();
			BindData();
		}

		private void GetSource()
		{
			sql = new SQLAccess();
			DataGrid1.DataSource = sql.GetUsers();
			DataGrid1.DataKeyField = "UserID";
			//sqlConnection1.ConnectionString = sql.GetConnectionString();
			//sqlDataAdapter1.Fill(dsPartner1);
		}
		private void BindData()
		{
			DataGrid1.DataBind();
		}

		private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			sql = new SQLAccess();
			DataSet dsUser = sql.GetUsers();
			DataView vwUser = new DataView(dsUser.Tables[0]);
			vwUser.Sort = e.SortExpression;
			DataGrid1.DataSource = vwUser;
			DataGrid1.DataKeyField = "UserID";
			BindData();
		}

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
			int RoleID = int.Parse(lstRoleID.SelectedValue);
			string Email = txtEmail.Text;  
			string Password = txtPassword.Text;  
			string Description = txtDesc.Text;
			string sType = txtType.Text;
			string sStatus = lstStatus.SelectedItem.ToString();
			
			string sUser = lstUser.SelectedItem.ToString();
			string sNickName = txtNickName.Text;
			
			

			if (sql != null)
				sql = new SQLAccess();
			//sql.InsertPost(sInsert);
			sql.InsertUserInfo(sUser, RoleID, Email, Password, sNickName, sStatus, sType, Description);
			
			GetSource();
			BindData();
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex = e.NewPageIndex;
			GetSource();
			BindData();
		}

		
	}
}
